from Tkinter import *
import re,threading

t = None

class window:
	def __init__(self,daft):
		self.daft = daft
		self.root = Tk() #create our tk window
		self.root.title('DAFT') #give it a title
		self.w = Text(self.root,relief=GROOVE,wrap=WORD,height=8,width=32) #this is the message display
		self.w.tag_config("bot",font="sans 10 bold")
		self.w.tag_config("man",font="sans 10")
		self.w.config(state=DISABLED)
		self.w.pack() #here w attach it to our tkinter window
		self.x = Entry(self.root) #here we have our text entry field
		self.x.pack() #and attach text entry to 
		self.root.bind_all('<Return>', self.boom)


	def boom(self,event):
		got = self.x.get()
		self.manr(got)
		r = self.daft.boom(got,"desktop","window")

	def botr(self,text):
		self.w.config(state=NORMAL)
		self.w.insert(END,"\n"+text,("bot"))
		self.w.see(END)
		self.w.config(state=DISABLED)
	def manr(self,text):
		self.w.config(state=NORMAL)
		self.w.insert(END,"\n"+text,("man"))
		self.w.see(END)
		self.w.config(state=DISABLED)

	def say(self,r):
		self.botr(r)
		self.x.delete(0,len(self.x.get()))
	def close(self):
		self.root.destroy()
		self.root.quit()


class winthread ( threading.Thread ):
	def __init__(self,daft):
		threading.Thread.__init__(self)
		self.daft = daft
	def run ( self ):
		self.w = window(self.daft)
		self.w.root.mainloop()
		self.w.close()
	def window(self):
		return self.w
	def close(self):
		self.w.close()

def tick():
	pass
def say(daft,r,who,w):
	if "window" in w.split(","):
		global t
		t.window().say(r)
def start(daft):
	if "window" in sys.argv or "quietwindow" in sys.argv:
		global t
		t = winthread(daft)
		t.start()
	return True
def stop():
	t.close()
